package com.shop.shoporder.OrderTimingTask;
import com.alibaba.fastjson.JSONObject;
import com.shop.shoporder.common.MessageConstants;
import com.shop.shoporder.common.Response;
import com.shop.shoporder.common.UserRequest;
import com.shop.shoporder.entity.order.Cart;
import com.shop.shoporder.entity.order.Order;
import com.shop.shoporder.service.CartService;
import com.shop.shoporder.service.ExportService;
import com.shop.shoporder.service.OrderReturnsService;
import com.shop.shoporder.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class ReturnOrder {
    @Autowired
    OrderService orderService;
    @Autowired
    CartService cartService;
    @Autowired
    OrderReturnsService orderReturnsService;
//    @Scheduled(cron="0 30 10 ? * *")//每天早上10.30触发导单
@Scheduled(fixedRate=300000)//5分钟一次
    public Response returOrder() {
    JSONObject jsonObject=null;
        String Ip = UserRequest.getServerIp();
    if (!Ip.contains("172.18.109.46")) {
            System.out.println("本地运行");
        return new Response(MessageConstants.MSG_ACCESS_DENY, null, 0, "1", null);
        }else {
            Order order1=new Order();
            order1.setOrderStatus("704");
            List<Order> orderList=orderService.selectByOrderStatus(order1);
            for (Order order: orderList) {
                String orderStatus=order.getOrderStatus()==null?"-1":order.getOrderStatus();
                if("707".equals(orderStatus)){
                    return new Response(MessageConstants.MSG_ACCESS_DENY, null, 0, "订单已处理退款", null);
                }
                List<Cart> cartList =cartService.selectListByOrderNo(order.getOrderNo());
                for (Cart cart:cartList) {
                    int isOrderFrom=cart.getRemark3()==null?-1:cart.getRemark3();
                    if(isOrderFrom==1){
                        return new Response(MessageConstants.MSG_ACCESS_DENY, null, 0, "已导出发货单请联系管理员",null);
                    }
                }

                double cash=order.getCash()==null?0:order.getCash();
                cash=0;
                if(cash>0){
                    //微信退款
                    return new Response(MessageConstants.MSG_ACCESS_DENY, jsonObject, "读取数据错误Wechat", null);
                }else {
                    jsonObject=orderReturnsService.reFund(order);
                    if(jsonObject!=null){
                        String code = jsonObject.get("code").toString();
                        if("000000".equals(code)){
                            order.setOrderStatus("707");
                            order.setRefundAdminid(1);
                            order.setRefundTime(new Date());
                            int resE=orderService.updateByPrimaryKeySelective(order);
                            if(resE>0) {
                                return new Response(MessageConstants.MSG_SUCCESS, jsonObject, "退款成功", null);
                            }else {
                                order.setOrderStatus("704");
                                order.setRefundAdminid(0);
                                order.setRefundTime(null);
                                order.setRefundError("退款订单状态更新有误!");
                                orderService.updateByPrimaryKeySelective(order);
                                return new Response(MessageConstants.MSG_ACCESS_DENY, jsonObject, "退款失败", null);
                            }
                        }else {
                            order.setRefundError("auto"+jsonObject.get("errorDetail").toString());
                            int resE=orderService.updateByPrimaryKeySelective(order);
                            return new Response(MessageConstants.MSG_ACCESS_DENY, jsonObject, jsonObject.get("errorDetail").toString(), null);
                        }
                    }else {
                        return new Response(MessageConstants.MSG_ACCESS_DENY, jsonObject, "退款失败", null);
                    }
                }

            }
        return new Response(MessageConstants.MSG_ACCESS_DENY, jsonObject, "退款失败", null);
        }
    }
}
